//
//  ShoppingViewController.m
//  FruitVIP
//
//  Created by 孙艳玲 on 15/5/21.
//  Copyright (c) 2015年 孙艳玲. All rights reserved.
//

#import "ShoppingViewController.h"


#define SCREEN_WIDTH ([UIScreen mainScreen].bounds.size.width)
#define SCREEN_HEIGHT ([UIScreen mainScreen].bounds.size.height)


@interface ShoppingViewController ()

@end

@implementation ShoppingViewController
{
    UILabel *_cntLabel;
    
}
- (void)viewDidAppear:(BOOL)animated

{
    
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0) {
             self.tabBarController.tabBar.hidden = YES;
        self.navigationController.interactivePopGestureRecognizer.enabled = NO;    //让rootView禁止滑动
        [self.myTableView reloadData];
        
    }
    
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:YES];
    self.tabBarController.tabBar.hidden = YES;
    self.navigationController.navigationBar.hidden = NO;
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:255.0/255.0 green:206.0 / 255.0 blue:0 / 255.0 alpha:1];
    self.tabBarController.selectedViewController = [self.tabBarController.viewControllers objectAtIndex:1];
    [self.myTableView reloadData];

   }

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
   
    
    self.view.backgroundColor =  [UIColor whiteColor];
    self.navigationItem.title = @"购物车";
//     [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor colorWithRed:241.0 / 255.0 green:204.0 / 255.0 blue:48.0 / 255.0 alpha:1]}];
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor colorWithRed:110.0 / 255.0 green:78.0 / 255.0 blue:71.0 / 255.0 alpha:1]}];
//    self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:40.0/255.0 green:40.0/255.0 blue:40.0/255.0 alpha:1];
      self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:255.0/255.0 green:206.0 / 255.0 blue:0 / 255.0 alpha:1];
    
    
    self.tabBarController.tabBar.hidden = YES;
    
    self.priceArray = [NSMutableArray arrayWithArray:@[@"60", @"80", @"100", @"50", @"120", @"90",@"39"]];
    self.cellArray = [NSMutableArray arrayWithArray:@[@"detailone.jpg",@"detailtwo.jpg", @"detailthree.jpg", @"detailfour.jpg", @"detailfive.jpg", @"detailsix.jpg"]];
    
    self.myTableView =[[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH , SCREEN_HEIGHT - 64 - 49  ) style:UITableViewStylePlain];
    self.myTableView.delegate = self;
    self.myTableView.dataSource = self;
//    self.myTableView.backgroundColor = [UIColor colorWithRed:158.0 / 255.0 green:147.0 / 255.0 blue:131.0 / 255.0 alpha:1];
    self.myTableView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.myTableView];
    
    
    
    UIImage *image = [UIImage imageNamed:nil];
    image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    UIButton *buttonBack = [UIButton buttonWithType:UIButtonTypeCustom];
    buttonBack.frame = CGRectMake(0, 5, 10, 20);
    [buttonBack setBackgroundImage:image forState:UIControlStateNormal];
    [buttonBack addTarget:self action:@selector(leftButtonAction) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:buttonBack];

   
    UIView *footView = [[UIView alloc] initWithFrame:CGRectMake(0, SCREEN_HEIGHT - 49 - 64, SCREEN_WIDTH, 49)];
    footView.backgroundColor = [UIColor blackColor];
    [self.view addSubview:footView];
    
    
    UIButton *firstButton = [UIButton buttonWithType:UIButtonTypeCustom];
    firstButton.frame = CGRectMake(10, 0, 40, 40);
    [firstButton setImage:[UIImage imageNamed:@"shouye.png"] forState:UIControlStateNormal];
    [firstButton addTarget:self action:@selector(firstButtonAction) forControlEvents:UIControlEventTouchUpInside];
    [footView addSubview:firstButton];
    
    UIButton *secondButton = [UIButton buttonWithType:UIButtonTypeCustom];
    secondButton.frame = CGRectMake(firstButton.frame.origin.x + firstButton.frame.size.width + 30, firstButton.frame.origin.y, 40, 40);
    [secondButton setImage:[UIImage imageNamed:@"heigh.png"] forState:UIControlStateNormal];
    [secondButton addTarget:self action:@selector(secondButtonAction) forControlEvents:UIControlEventTouchUpInside];
    [footView addSubview:secondButton];
    // label数字 显示购物车的个数
    UIColor *customColor  = [UIColor colorWithRed:237/255.0 green:20/255.0 blue:91/255.0 alpha:1.0f];
    _cntLabel = [[UILabel alloc] initWithFrame:CGRectMake(secondButton.frame.origin.x + secondButton.frame.size.width - 8, 5, 20, 20)];
    _cntLabel.textColor = [UIColor whiteColor];
    _cntLabel.textAlignment = NSTextAlignmentCenter;
    _cntLabel.font = [UIFont boldSystemFontOfSize:13];
    _cntLabel.backgroundColor = [UIColor whiteColor];
    _cntLabel.layer.cornerRadius = CGRectGetHeight(_cntLabel.bounds)/2;
    _cntLabel.layer.masksToBounds = YES;
    _cntLabel.backgroundColor = customColor;
//    _cntLabel.layer.borderWidth = 1.0f;
//    _cntLabel.layer.borderColor = customColor.CGColor;
    _cntLabel.text = [NSString stringWithFormat:@"%ld", self.goodsCount];
      [footView addSubview:_cntLabel];
    NSLog(@"self.goodsCount = %ld", self.goodsCount);
    
    UIButton *thirdButton = [UIButton buttonWithType:UIButtonTypeCustom];
    thirdButton.frame = CGRectMake(secondButton.frame.origin.x + secondButton.frame.size.width + 30, secondButton.frame.origin.y, 40, 40);
    [thirdButton setImage:[UIImage imageNamed:@"me.png"] forState:UIControlStateNormal];
    [thirdButton addTarget:self action:@selector(thirdButtonAction) forControlEvents:UIControlEventTouchUpInside];
    [footView addSubview:thirdButton];
    
    UIButton *footButton = [UIButton buttonWithType:UIButtonTypeCustom];
    footButton.frame = CGRectMake(SCREEN_WIDTH - SCREEN_WIDTH/3.3, 0, SCREEN_WIDTH/3.3, footView.frame.size.height);
    footButton.backgroundColor = [UIColor colorWithRed:242.0 / 255.0 green:204.0 / 255.0 blue:49.0 / 255.0 alpha:1];
    [footButton setTitle:@"去结算" forState:UIControlStateNormal];
    [footButton setTitleColor:[UIColor colorWithRed:71.0 / 255.0 green:61.0 / 255.0 blue:55.0 / 255.0 alpha:1] forState:UIControlStateNormal];
    footButton.titleLabel.font = [UIFont systemFontOfSize:17];
    [footButton addTarget:self action:@selector(footButtonAction) forControlEvents:UIControlEventTouchUpInside];
    [footView addSubview:footButton];

    
}

- (void)leftButtonAction
{
    
    [self.navigationController popToRootViewControllerAnimated:YES];
    
//    self.tabBarController.tabBar.hidden = NO;
   
}

- (void)firstButtonAction
{
    [self leftButtonAction];
    self.tabBarController.selectedViewController = [self.tabBarController.viewControllers objectAtIndex:0];
    self.tabBarController.tabBar.hidden = NO;
}
- (void)secondButtonAction
{

    self.tabBarController.selectedViewController = [self.tabBarController.viewControllers objectAtIndex:1];
    }
- (void)thirdButtonAction
{
    self.tabBarController.selectedViewController = [self.tabBarController.viewControllers objectAtIndex:2];
}




- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}
- (void)allPrice:(NSInteger)allPrice
{
       //把整型转化为字符串
    self.string = [NSString stringWithFormat:@"%ld", allPrice];
    self.allMoneyLabel.text = self.string;
    NSLog(@"%ld", allPrice);
    [self.myTableView reloadData];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
        return self.cellArray.count;
    }
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
   
       if (indexPath.section == 0) {
       NSString *cellIdentifier = [NSString stringWithFormat:@"cell%ld", indexPath.row];
        ShoppingTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
                  if (cell == nil) {
                cell = [[ShoppingTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
            }
            cell.myDelegate = self;
            cell.deleteDelegate = self;
            cell.priceLabel.text = [self.priceArray objectAtIndex:indexPath.row];
            //字符串拼接
        //    NSString *str = @"￥";
        //    cell.priceLabel.text = [str stringByAppendingString:cell.priceLabel.text];
        //    cell.backgroundColor = [UIColor colorWithRed:176.0/255.0 green:164.0/255.0 blue:144.0/255.0 alpha:1];
            cell.selectedBackgroundView.backgroundColor = [UIColor colorWithRed:158.0 / 255.0 green:147.0 / 255.0 blue:131.0 / 255.0 alpha:1];
            NSString *str = [self.cellArray objectAtIndex:indexPath.row];
            cell.foodImageView.image = [UIImage imageNamed:str];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
       
            return cell;

    }
    else
    {
       
//        static NSString *cellPrice = @"cellAll";
        NSString *cellPrice = [NSString stringWithFormat:@"cellAll%ld", indexPath.row];
        
        UITableViewCell *cellAll = [tableView dequeueReusableCellWithIdentifier:cellPrice];
        if (cellAll == nil) {
            cellAll = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellPrice];
            UILabel *priceLabel = [[UILabel alloc] initWithFrame:CGRectMake(cellAll.frame.size.width/2.5,SCREEN_WIDTH/9, cellAll.frame.size.width/4.5, SCREEN_WIDTH/9)];
            priceLabel.text = @"总金额：";
            priceLabel.font = [UIFont systemFontOfSize:17];
//            priceLabel.backgroundColor = [UIColor yellowColor];
            [cellAll addSubview:priceLabel];
            
            UILabel *symbolLabel = [[UILabel alloc] initWithFrame:CGRectMake(priceLabel.frame.origin.x + priceLabel.frame.size.width, priceLabel.frame.origin.y, 15, priceLabel.frame.size.height)];
            symbolLabel.text = @"￥";
            symbolLabel.font = [UIFont systemFontOfSize:17];
            [cellAll addSubview:symbolLabel];
            
            
            self.allMoneyLabel = [[UILabel alloc] initWithFrame:CGRectMake(symbolLabel.frame.origin.x + symbolLabel.frame.size.width, symbolLabel.frame.origin.y - 10, cellAll.frame.size.width - priceLabel.frame.size.width - 15, symbolLabel.frame.size.height + 10)];
            self.allMoneyLabel.text = @"0";
            self.allMoneyLabel.font = [UIFont systemFontOfSize:25];
//            allMoneyLabel.backgroundColor = [UIColor yellowColor];
            [cellAll addSubview:self.allMoneyLabel];
             [self.myTableView reloadData];
            
        }
        
                return cellAll;
    }
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        return SCREEN_WIDTH / 3 + 20;
    }
    return SCREEN_WIDTH/3;
    
}

- (void)footButtonAction
{
    NSLog(@"去结算");
    [self.myTableView reloadData];
    CountPayViewController *countPayVC = [[CountPayViewController alloc] init];
    countPayVC.allMoney = self.allMoneyLabel.text;
    [self.navigationController pushViewController:countPayVC animated:YES];
    self.tabBarController.tabBar.hidden = NO;
    
  
}


//重写系统的编辑按钮方法
- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
    [super setEditing:editing animated:animated];
    [self.myTableView setEditing:editing animated:animated];
}

//编辑类型
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewCellEditingStyleDelete;
}

//允许编辑
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}

//实现编辑逻辑逻辑

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        [self.cellArray removeObjectAtIndex:indexPath.row];
         [self.myTableView deleteRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationLeft];
    }
    else
    {
    if (self.cellArray.count == 0) {
        [self.cellArray removeObjectAtIndex:indexPath.section];
        [self.myTableView deleteSections:[NSIndexSet indexSetWithIndex:indexPath.section] withRowAnimation:UITableViewRowAnimationLeft];
      }
    }
}

- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return @"删除";
}

- (void)deleteButton
{
    NSLog(@"删除");
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示信息" message:@"是否删除" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:@"取消", nil];
    [alert show];
}
- (void)btnClick:(UITableViewCell *)cell andFlag:(int)flag andGoodsInfo:(ShoppingTableViewCell *)shoppingCell button:(UIButton *)button
{
//    [self.myTableView reloadData];
//    NSString *str = shoppingCell.countLabel.text;
//    NSInteger count = [str integerValue];
    switch (flag) {
        case 11:
        {
            if (shoppingCell.cutBUtton.tag == 1002) {
                self.allMoneyLabel.text = self.string;

                [self.myTableView reloadData];

            }
        }
            break;
        case 12:
        {
            [self.myTableView reloadData];

        }
            
        default:
            
            break;
    }
    
    self.allMoneyLabel.text = self.string;
    [self.myTableView reloadData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
